package com.peopledelete.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.peopledelete.mapper.DateMapper;
import com.peopledelete.mapper.DayrecordsMapper;
import com.peopledelete.pojo.Date;
import com.peopledelete.pojo.Dayrecords;
import com.peopledelete.service.DateService;
import com.peopledelete.service.DayrecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author cyt
 * @create 2020-04-27 1:26
 */

@RestController
public class ToalController {

    @Autowired
    DayrecordsMapper dayrecordsMapper;

    @Autowired
    DateService service;

    /**
     * 返回每天的人流总数
     *
     * @return
     */
    @Autowired
    DateMapper dateMapper;

    @Autowired
    DayrecordsService dayrecordsService;

    //date表往后台进行映射
    @RequestMapping("/date")
    @ResponseBody
    public String Send03() {
        List<Dayrecords> list = dayrecordsService.list(null);
        String s = JSON.toJSONStringWithDateFormat(list, "yyyy-MM-dd");
        return s;
    }

    @ResponseBody
    @RequestMapping("/weakNum")
    public String weakNum() {

        ArrayList<Dayrecords> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance(); // 可以对每个时间域单独修改
        // 1-7分别代表 -- 星期日,星期一,星期二,星期三,星期四,星期五,星期六
        int week = calendar.get(calendar.DAY_OF_WEEK);
        System.out.println(week);
        // week = 1;
        // 1 代表周日
        if (week == 1) {
            for (int i = 1; i < 8; i++) {
                Calendar c = Calendar.getInstance(); // 可以对每个时间域单独修改
                c.add(c.DATE, -i + 1);
                java.util.Date time = c.getTime();
                String format = new SimpleDateFormat("yyyy-MM-dd").format(time);
                QueryWrapper<Dayrecords> wrapper = new QueryWrapper<>();
                wrapper.eq("date", format);
                Dayrecords dayrecords = dayrecordsMapper.selectOne(wrapper);
                list.add(dayrecords);
                System.out.println(dayrecords);
            }
        } else {
            for (int i = 1; i < week; i++) {
                Calendar cal = Calendar.getInstance(); // 可以对每个时间域单独修改
                cal.add(Calendar.DATE, -i + 1);
                java.util.Date time = cal.getTime();
                String format = new SimpleDateFormat("yyyy-MM-dd").format(time);
                QueryWrapper<Dayrecords> wrapper = new QueryWrapper<>();
                wrapper.eq("date", format);
                Dayrecords dayrecords = dayrecordsMapper.selectOne(wrapper);
                list.add(dayrecords);
                System.out.println(dayrecords);
            }
        }
        System.out.println(list.toString());
        String weaknum = JSON.toJSONStringWithDateFormat(list, "yyyy-MM-dd");
        return weaknum;
    }
}
